#include "GL/glut.h"
#include "demo.h"
#include "time.h"
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <iostream>
using namespace std;

const double thePAI=3.1416;

void geardemo()
{
	static GLuint clgearnum = 0;
	if(0 == clgearnum)
	{
		clgearnum = glGenLists(1);
		glNewList(clgearnum, GL_COMPILE);
		{	
			glDisable(GL_TEXTURE_2D);
			
			//坐标
			glColor3f(1.0, 0.0, 0.0);
			glBegin(GL_LINES);
			glVertex3f(0.0,0.0,0.0);
			glVertex3f(1.0,0.0,0.0);   
			glEnd();

			glColor3f(0.0, 1.0, 0.0);
			glBegin(GL_LINES);
			glVertex3f(0.0,1.0,0.0);
			glVertex3f(0.0,0.0,0.0);
			glEnd();

			glColor3f(0.0, 0.0, 1.0);
			glBegin(GL_LINES);
			glVertex3f(0.0, 0.0, 1.0);
			glVertex3f(0.0, 0.0, 0.0);
			glEnd();
			
			float agl;
			float outradius=2.0f;
			float inradius=0.5f;
			float zlength = 0.0f;
			double delta = 0.1;

			glColor3f(1.0, 0.0, 0.0);
			glBegin(GL_TRIANGLE_STRIP);
			for(agl=0.0; agl<(2*thePAI); agl+=delta)
			{
				glVertex3f(outradius*cos(agl), outradius*sin(agl), -zlength);
				glVertex3f(inradius*cos(agl), inradius*sin(agl), -zlength);
			}
			glVertex3f(outradius*cos(0.0), outradius*sin(0.0), -zlength);
			glVertex3f(inradius*cos(0.0), inradius*sin(0.0), -zlength);
			glEnd();

			const int gearnum = 36;
			float deltaPerGear = (thePAI)/gearnum;
			float gearheight = 0.2f;
			agl=0.0f;
			glColor3f(0.0, 0.0, 1.0);
			glBegin(GL_TRIANGLES);
			for(int i=0; i< gearnum; ++i)
			{
				glVertex3f((outradius+gearheight)*cos(agl), (outradius+gearheight)*sin(agl), -zlength);
				glVertex3f(outradius*cos(agl), outradius*sin(agl), -zlength);
				agl += deltaPerGear;
				glVertex3f(outradius*cos(agl), outradius*sin(agl), -zlength);
				glVertex3f(outradius*cos(agl), outradius*sin(agl), -zlength);
				agl += deltaPerGear;
				glVertex3f(outradius*cos(agl), outradius*sin(agl), -zlength);
				glVertex3f((outradius+gearheight)*cos(agl), (outradius+gearheight)*sin(agl), -zlength);
			}
			glEnd();
		}
		glEndList();	
	}
	glCallList(clgearnum);
}

void newdemo()
{
	static GLuint clstnum = 0;
	if(0 == clstnum)
	{
		cout<<"CALL LIST CREATE"<<endl;
		clstnum = glGenLists(1);
		glNewList(clstnum, GL_COMPILE);
		{	
			glDisable(GL_TEXTURE_2D);
			
			//坐标
			glColor3f(1.0, 0.0, 0.0);
			glBegin(GL_LINES);
			glVertex3f(0.0,0.0,0.0);
			glVertex3f(1.0,0.0,0.0);   
			glEnd();

			glColor3f(0.0, 1.0, 0.0);
			glBegin(GL_LINES);
			glVertex3f(0.0,1.0,0.0);
			glVertex3f(0.0,0.0,0.0);
			glEnd();

			glColor3f(0.0, 0.0, 1.0);
			glBegin(GL_LINES);
			glVertex3f(0.0, 0.0, 1.0);
			glVertex3f(0.0, 0.0, 0.0);
			glEnd();
			
			//螺杆
			double agl = 0.0;
			double delta = 0.1;
			double zlength=1.5;
			double radius = 0.5;
			/* 外壁
			for(double lenz=-zlength; lenz<zlength; lenz+=delta)
			{
				glColor3f((zlength-lenz)/4.0, 0.0, 1.0);
				glBegin(GL_TRIANGLE_STRIP);
				for(agl=0.0; agl<2*thePAI; agl+=delta)
				{
					glVertex3f(radius*cos(agl), radius*sin(agl), lenz);
					glVertex3f(radius*cos(agl), radius*sin(agl), lenz+delta);
				}
				glVertex3f(radius*cos(0.0), radius*sin(0.0), lenz);
				glVertex3f(radius*cos(0.0), radius*sin(0.0), lenz+delta);
				glEnd();
			}
			//*/
		
			double outradius = radius;
			radius = 0.4;
			double inradius = radius;
			
			/*
			r=2*pi*a
			a
			x = r*cos(a)
			y = r*sin(a)
			*/
			//* 渐开线
			glColor3f(1.0, 0.0, 0.0);
			//glBegin(GL_POINTS);
			glBegin(GL_LINE_STRIP);
			for(agl=0.0; agl<2*thePAI; agl+=delta)
			{
				double ther = 0.1*2.0*thePAI*agl;
				glVertex3f(ther*cos(agl), ther*sin(agl), 0);
				//glVertex3f(radius*cos(agl), radius*sin(agl), 0);
			}
			glEnd();
			//*/
			
			//* 内壁
			for(double lenz=-zlength; lenz<zlength; lenz+=delta)
			{
				glColor3f(1.0-(zlength-lenz)/4.0, 0.0, 1.0);
				glBegin(GL_TRIANGLE_STRIP);
				for(agl=0.0; agl<2*thePAI; agl+=delta)
				{
					glVertex3f(radius*cos(agl), radius*sin(agl), lenz);
					glVertex3f(radius*cos(agl), radius*sin(agl), lenz+delta);
				}
				glVertex3f(radius*cos(0.0), radius*sin(0.0), lenz);
				glVertex3f(radius*cos(0.0), radius*sin(0.0), lenz+delta);
				glEnd();
			}
			//*/

			//* 上环
			glColor3f(0.0, 1.0, 0.0);
			glBegin(GL_TRIANGLE_STRIP);
			for(agl=0.0; agl<(2*thePAI); agl+=delta)
			{
				glVertex3f(outradius*cos(agl), outradius*sin(agl), zlength);
				glVertex3f(inradius*cos(agl), inradius*sin(agl), zlength);
			}
			glVertex3f(outradius*cos(0.0), outradius*sin(0.0), zlength);
			glVertex3f(inradius*cos(0.0), inradius*sin(0.0), zlength);
			glEnd();
			
			//* 下环
			glColor3f(1.0, 1.0, 0.0);
			glBegin(GL_TRIANGLE_STRIP);
			for(agl=0.0; agl<(2*thePAI); agl+=delta)
			{
				glVertex3f(outradius*cos(agl), outradius*sin(agl), -zlength);
				glVertex3f(inradius*cos(agl), inradius*sin(agl), -zlength);
			}
			glVertex3f(outradius*cos(0.0), outradius*sin(0.0), -zlength);
			glVertex3f(inradius*cos(0.0), inradius*sin(0.0), -zlength);
			glEnd();

			//螺旋
			double gearhight = 0.2;
			double gearthick = 0.1;
			double gearlooplength = 0.5;
		
			//*
			glColor3f(1.0, 1.0, 0.0);
			glBegin(GL_TRIANGLE_STRIP);
			double nowz = 0.0;
			for(agl=0.0; true; agl+=delta)
			{
				nowz = agl*(gearlooplength/(2*thePAI))-zlength;
				glVertex3f(outradius*cos(agl), outradius*sin(agl), nowz);
				glVertex3f((outradius+gearhight)*cos(agl), (outradius+gearhight)*sin(agl), nowz);
				if(nowz > zlength)
				{
					break;	
				}
			}
			glEnd();
			
			glColor3f(0.0, 1.0, 1.0);
			glBegin(GL_TRIANGLE_STRIP);
			for(agl=0.0; true; agl+=delta)
			{
				nowz = agl*(gearlooplength/(2*thePAI))-zlength;
				glVertex3f(outradius*cos(agl), outradius*sin(agl), nowz+gearthick);
				glVertex3f((outradius+gearhight)*cos(agl), (outradius+gearhight)*sin(agl), nowz);
				if(nowz > zlength)
				{
					break;	
				}
			}
			glEnd();
			//*/
		}
		glEndList();	
	}
	glCallList(clstnum);
}

